const Generator = require('yeoman-generator');
const fse = require('fs-extra');
const path = require('path');
const ora = require('ora'); // 通过ora、chalk模块也进行一些视觉美化
const chalk = require('chalk');
const child_process = require('child_process');


module.exports = class CloneCli extends Generator {
    prompting() {
        return this.prompt([
            {
                type: 'input',
                name: 'name',
                message: 'Your project name',
                default: this.appname
            }, {
                type: 'input',
                name: 'templates',
                message: 'Do you want to use online templates?',
                default: 'n'
            }
        ]).then(answers => {
            this.answers = answers;
        })
    }

    async writing() {
        /**
         * this.sourceRoot() ==> \generators\app\templates
         * this.destinationPath() ==> 目标路径
         */

        const chunkPaths = await fse.readdir(this.sourceRoot());
        const sourceRoot = this.sourceRoot();
        if ('n' === this.answers.templates.toLowerCase()) {
            // this.fs.copyTpl 模板路径,目标路径,传递参数
            // this.fs.copyTpl(this.templatePath('./src/index.html'), this.destinationPath('./src/index.html'), this.answers);

            for (const chunkPath of chunkPaths) {
                fse.copy(path.join(sourceRoot, chunkPath), this.destinationPath(chunkPath), function (err) {
                    if (err) return console.log(chalk.green(err));
                }); //拷贝模板目录
            }
            console.log(chalk.green('copy templates is success'));
        } else {
            child_process.exec('git clone https://gitee.com/front_clone/fed-e-task-01-01.git', (err, stdout, stderr) => {
                if (err) {
                    console.log(chalk.red(err));
                } else {
                    console.log(chalk.green('git clone templates is success'));
                }
            });
        }
    }

    install() {

    }
}